# coding: utf-8
from typing import Union

from PySide6.QtCore import Qt
from PySide6.QtGui import QMouseEvent, QColor, QIcon
from PySide6.QtWidgets import QVBoxLayout, QHBoxLayout, QWidget
from qfluentwidgets import CardWidget, SimpleCardWidget, FluentIconBase, IconWidget, BodyLabel
from qfluentwidgets.common.animation import BackgroundAnimationWidget


class StreamCardWidget(CardWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setClickEnabled(True)

    def mouseReleaseEvent(self, e: QMouseEvent):
        BackgroundAnimationWidget.mouseReleaseEvent(self, e)
        if e.button() == Qt.MouseButton.LeftButton and self.isClickEnabled() and self.rect().contains(e.pos()):
            self.clicked.emit()


class CustomSimpleCardWidget(SimpleCardWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setClickEnabled(False)

    def mouseReleaseEvent(self, e: QMouseEvent):
        BackgroundAnimationWidget.mouseReleaseEvent(self, e)
        if e.button() == Qt.MouseButton.LeftButton and self.isClickEnabled() and self.rect().contains(e.pos()):
            self.clicked.emit()

    def _normalBackgroundColor(self):
        return QColor(255, 255, 255, 13)


class ExpandCardWidget(CustomSimpleCardWidget):
    def __init__(self, icon: Union[str, QIcon, FluentIconBase], text: str, parent=None):
        super().__init__(parent)
        self.verticalLayout = QVBoxLayout(self)
        self.horizontalLayout = QHBoxLayout()
        self.iconWidget = IconWidget(icon, self)
        self.bodyLabel = BodyLabel(text, self)
        self.subWidget = QWidget(self)
        self.subLayout = QHBoxLayout(self.subWidget)

        self.__initWidget()

    def __initWidget(self):
        self.setFixedHeight(50)
        self.iconWidget.setFixedSize(16, 16)

        self.verticalLayout.setContentsMargins(16, 5, 16, 5)
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout.setSpacing(11)
        self.verticalLayout.setSpacing(11)

        self.horizontalLayout.addWidget(self.iconWidget, 0, Qt.AlignmentFlag.AlignLeft)
        self.horizontalLayout.addWidget(self.bodyLabel, 1, Qt.AlignmentFlag.AlignLeft)
        self.horizontalLayout.addWidget(self.subWidget, 1)
        self.verticalLayout.addLayout(self.horizontalLayout)

        self.subLayout.setContentsMargins(0, 0, 0, 0)
        self.subLayout.setSpacing(8)

    def addWidget(self, widget, *args, **kwargs):
        self.subLayout.addWidget(widget, *args, **kwargs)
